<?php
namespace app\admin\controller;
use think\Db;
class Adminpower extends Base
{   
    public function lists(){
        //ajax查询数据
        if( is_post() ){
            $page     = input('page');
            $keyword  = input('keyword');
            $type    = input('type');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    admin_power
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $type != '100all' ) {
                $sql .= " and type = ?";
                $param[] = $type;
            }
            if ( !empty($keyword) ) {
                $sql .= " and title like ?";
                $param[] = "%{$keyword}%";
            }
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
			$page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by id desc limit $start, $page_size";
			$field       = "*";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['type']           =  c('type.admin_power_type', $v['type']);
                $list[$k]['action']         =  '<a class="ml-5" tid="'.$v['id'].'" _width=500 _height=500 _url="'.url('adminpower/edit', 'id='.$v['id']).'" msg="编辑权限" onclick="com_page(this)" href="javascript:;" title="编辑"><i class="Hui-iconfont">&#xe6df;</i></a>';
                $list[$k]['action']        .=  com_action_del( url("adminpower/delete"), $v['id'], "你确定要删除{$v['title']}吗？" );
            }
			$com_body    = com_build_table($list, array("checkbox","id","title", "type", "module", "controller", "actions", "action"));
			$com_page    = com_build_page( $page_data );
			$data = array(
				"com_body"  => $com_body,
				"com_page"  => $com_page,
			);
            $config = [
                'data'   => $data,
            ];
			com_check($config);
        }       
        $data = [
            'nav'       => ['权限管理', '列表'],
            'type'      => ['100all'=>'全部分类'] + c('type.admin_power_type'),
        ];
        return view('', $data);
    }
    
    //添加
    public function add(){
        if( is_post() ){
            $data = input();  #post数据
            //验证
            $rule = [
                'type'      =>  'number',
                'title'      =>  'require',
                'controller'       =>  'require',
                'actions'       =>  'require',
            ];
            $msg = [
                'type.number'      =>  '请选择分类',
                'title.require'      =>  '权限标题不能为空',
                'controller.require' =>  '权限操作类不能为空',
                'actions.require'    =>  '权限操作方法名不能为空',
            ];
            com_validate( $rule, $msg, $data, true );
            Db::name('admin_power')->insertGetId($data);
            $config = [
                'msg' => '添加权限成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        } 
        $data = [
            'type'      => ['100all'=>'全部分类'] + c('type.admin_power_type'),
        ];
        return view('', $data);
    }
    
    
    //编辑
    public function edit( $id ){
        if( is_post() ){
            $data = input();  #post数据
            //验证
            $rule = [
                'type'      =>  'number',
                'title'      =>  'require',
                'controller'       =>  'require',
                'actions'       =>  'require',
            ];
            $msg = [
                'type.number'      =>  '请选择分类',
                'title.require'        =>  '权限标题不能为空',
                'controller.require'   =>  '权限操作类不能为空',
                'actions.require'      =>  '权限操作方法名不能为空',
            ];
            com_validate( $rule, $msg, $data, true );
            Db::name('admin_power')->update($data);
            $config = [
                'suc' => [
                    'msg' => '编辑权限成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $data = [
            'type'      => ['100all'=>'全部分类'] + c('type.admin_power_type'),
            'info'=> Db::name('admin_power')->find($id),
        ];
        return view('', $data);
    }
    
    //删除
    public function delete(){
        if( is_post() ){
            $id  =  input('id');
            $a   = new \app\admin\model\AdminPower();
            
            if( $a->checkDel($id) )
                Db::name('admin_power')->delete($id);
            
            $config = [
                'suc' => [
                    'msg'       => '删除成功!',
                    'js_action' => 'alert|reload',
                ],  
                'err' => [
                    'js_action' => 'alert',
                ]
            ];
            com_check($config);
        }
    }
    
    //批量删除
    public function delall(){
        if( is_post() ){
            
            $ids = input('ids/a');
            $a   = new \app\admin\model\AdminPower();
            
            foreach($ids as $id){
                if( $a->checkDel($id) ){
                       Db::name('admin_power')->delete($id);
                }
            }
            $config = [
                'err' => [
                    'js_action' => "alert|reload",
                ],
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check($config);
        }
    }
}